﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameFramework.GameLogic;
using Microsoft.Xna.Framework;
using MyRaceGame.GameLogic.InputControl;
using Microsoft.Xna.Framework.Input;
using GameFramework.Controller;
using GameFramework.Camera;
using GameFramework.ModelManager;
using Microsoft.Xna.Framework.Graphics;

namespace MyRaceGame.GameLogic.GameStates
{
    public class RaceState : RaceGameState
    {
        public RaceState(RaceGame raceGame)
            : base(raceGame)
        {
            // Set human racer's animation from "Idle" to "Run"
            RacerAnimationPlayer rap = RaceController.HumanRacer.AnimationPlayer as RacerAnimationPlayer;
            rap.CurrentAnimation = rap.RunAnimation;
            setRacerSpeeds();
            RaceGameState.LoadTextureFrom(raceGame, ref pauseBackTex, "MenuImages", "paused_back");
        }

        protected override void UpdateKeyboard(GameTime gameTime)
        {
            if (raceGame.CameraMode == CameraMode.ChaseCamera && InputController.KeyPressed(Keys.Q))
                raceGame.ChangeTrackedRacer();

            if (InputController.KeyPressed(Keys.C))
                raceGame.ChangeCameraMode(CameraMode.ChaseCamera);

            if (InputController.KeyPressed(Keys.F))
                raceGame.ChangeCameraMode(CameraMode.FreeCamera);

            if (raceGame.CameraMode != CameraMode.FreeCamera && InputController.KeyPressed(Keys.D))
                debugModeRequest = true;

            if (InputController.KeyPressed(Keys.N))
                raceGame.ShowText = !raceGame.ShowText;

            if (InputController.KeyPressed(Keys.T))
                raceGame.ToggleFullScreen();
        }

        protected override void UpdateTransition(GameTime gameTime)
        {
            if (debugModeRequest)
                GameState = new DebugState(raceGame);
            else if (RaceController.RaceFinished)
                GameState = new FinishState(raceGame);
        }

        protected override void UpdateRacers(GameTime gameTime)
        {
            raceGame.UpdateRacers(gameTime);
        }

        protected override void DrawText(GameTime gameTime)
        {
            TextDrawController.Begin(TextAlignment.Right);

            TextDrawController.WriteLine("Standing = {0}/{1}", RaceController.HumanRacerStanding,
                RaceController.Racers.Count);
            TextDrawController.WriteLine("Lap = {0}/{1}", RaceController.CurrentLap,
                RaceController.Laps);

            TextDrawController.End();
        }

        void setRacerSpeeds()
        {
            foreach (RacerCharacter racer in RaceController.Racers)
                if (!racer.IsHuman)
                {
                    racer.Speed = RandomHelper.RandomBetween(racer.RaceTrack.TrackLength / 16, 
                        racer.RaceTrack.TrackLength / 8);
                }
        }

        protected override void DrawingAdditional(GameTime gameTime)
        {
            raceGame.DrawRaceTrack();

            if (!raceGame.Paused)
                return;

            raceGame.SpriteBatch.Begin();

            raceGame.SpriteBatch.Draw(pauseBackTex, Vector2.Zero, Color.White);

            raceGame.SpriteBatch.End();
        }

        protected override void DrawRacers(GameTime gameTime)
        {
            raceGame.DrawModels();
        }

        bool debugModeRequest = false;
        static Texture2D pauseBackTex;
    }
}
